package com.fgxia.admin.controller.user;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.business.domain.bo.SysUserLoginDriverBo;
import com.ruoyi.business.domain.bo.admin.AdminSysUserLoginDriverBo;
import com.ruoyi.business.domain.vo.SysUserLoginDriverVo;
import com.ruoyi.business.domain.vo.admin.AdminSysUserLoginDriverVo;
import com.ruoyi.business.service.ISysUserLoginDriverService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 司机信息
 *
 * @author Fgxia LD 99
 * @date 2024-04-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userLoginDriver")
public class AdminSysUserLoginDriverController extends BaseController {

    private final ISysUserLoginDriverService iSysUserLoginDriverService;

    /**
     * 查询司机信息列表
     */
    @SaCheckPermission("business:userLoginDriver:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserLoginDriverVo> list(SysUserLoginDriverBo bo, PageQuery pageQuery) {
        return iSysUserLoginDriverService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出司机信息列表
     */
    @SaCheckPermission("business:userLoginDriver:export")
    @Log(title = "司机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserLoginDriverBo bo, HttpServletResponse response) {
        List<SysUserLoginDriverVo> list = iSysUserLoginDriverService.queryList(bo);
        ExcelUtil.exportExcel(list, "司机信息", SysUserLoginDriverVo.class, response);
    }

    /**
     * 获取司机信息详细信息
     *
     * @param driverId 主键
     */
    @SaCheckPermission("business:userLoginDriver:query")
    @GetMapping("/{driverId}")
    public R<AdminSysUserLoginDriverVo> getInfo(@NotNull(message = "主键不能为空")
                                                @PathVariable Long driverId) {
        return R.ok(iSysUserLoginDriverService.queryAdminById(driverId));
    }

    /**
     * 新增司机信息
     */
    @SaCheckPermission("business:userLoginDriver:add")
    @Log(title = "司机信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AdminSysUserLoginDriverBo bo) {

        String checkRepeatInfo = iSysUserLoginDriverService.checkRepeatInfo(bo);

        if (StringUtils.isNotBlank(checkRepeatInfo)) {
            return R.fail("新增司机信息'" + bo.getRealName() + "'失败，" + checkRepeatInfo);
        }

        return toAjax(iSysUserLoginDriverService.insertByAdminBo(bo));
    }

    /**
     * 修改司机信息
     */
    @SaCheckPermission("business:userLoginDriver:edit")
    @Log(title = "司机信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AdminSysUserLoginDriverBo bo) {
        String checkRepeatInfo = iSysUserLoginDriverService.checkRepeatInfo(bo);

        if (StringUtils.isNotBlank(checkRepeatInfo)) {
            return R.fail("修改司机信息'" + bo.getRealName() + "'失败，" + checkRepeatInfo);
        }

        return toAjax(iSysUserLoginDriverService.updateByAdminBo(bo));
    }

    /**
     * 删除司机信息
     *
     * @param driverIds 主键串
     */
    @SaCheckPermission("business:userLoginDriver:remove")
    @Log(title = "司机信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{driverIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] driverIds) {
        return toAjax(iSysUserLoginDriverService.deleteWithValidByIds(Arrays.asList(driverIds), true));
    }
}
